#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <SPI.h>
#include <Wire.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "DHT.h"


/*****************DHT11**************************/
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
/****************WIFI****************************/
const char *ssid     = "test19";           //WIFI名
const char *password = "12345678";         //密码
unsigned int localPort = 8887;             //本地端口号
WiFiUDP Udp;
char TxBuffer[50] = {0};
char RxBuffer[50] = {0};
char packetBuffer1[255];  //buffer to hold incoming packet
char packetBuffer2[255];
char ReplyBuffer1[] = ""; // a string to send back
char ReplyBuffer2[] = "";

float temperature = 0;     //存放温度的变量
float humidity    = 0;     //存放湿度的变量
float light       = 0;

int   T_Threshold = 0;
int   H_Threshold = 0; 
/*************开关控制*****************/
#define T_SWITCH_PIN  13
#define H_SWITCH_PIN  15

int timCnt = 0;
void setup()
{
        Serial.begin(115200);       //初始化串口通信，并将波特率设置为115200
        pinMode(T_SWITCH_PIN,OUTPUT);
        pinMode(H_SWITCH_PIN,OUTPUT);

        digitalWrite(T_SWITCH_PIN,LOW);
        digitalWrite(H_SWITCH_PIN,LOW);
        
        WiFi.mode(WIFI_AP);         //WIFI设为AP模式
        WiFi.begin(ssid, password); //连接WIFI热点
        
        while (WiFi.status() != WL_CONNECTED)  // 等待WIFI连接
        { 
                delay(100);
                Serial.print("."); //如果没有连通向串口发送.....
        }
        Serial.print("Connected! IP address: ");
        Serial.println(WiFi.localIP());
        Serial.printf("UDP server on port %d\n", localPort);
        Udp.begin(localPort);
        dht.begin();
}

//一直循环执行:
void loop()
{
        /**获取温湿度，后在串口打印出数据**/
        temperature = dht.readTemperature();
        humidity    = dht.readHumidity();

        light = analogRead(A0);
        light = (light*3.3f)/1023;
        if(timCnt == 8){
           timCnt = 0;
           sprintf(TxBuffer,"%f#%f#%f\n",temperature,humidity,light);
           Udp.beginPacket("DESKTOP-7MA2MU6", 7777);
           Udp.write(TxBuffer);
           Udp.endPacket();
        }

        int packetSize = Udp.parsePacket();  // 在这里加入主要程序代码，重复执行:
        if (packetSize)
        {
                Serial.print("Received packet of size ");
                Serial.println(packetSize);
                Serial.print("From ");
                IPAddress remoteIp = Udp.remoteIP(); //获取远程广播包发送IP
                Serial.print(remoteIp);
                Serial.print(", port ");
                Serial.println(Udp.remotePort());

                Udp.read(RxBuffer,packetSize);
                printf("%s\n",RxBuffer);
                sscanf(RxBuffer,"%d#%d",&T_Threshold,&H_Threshold);
                printf("%d,%d\r\n",T_Threshold,H_Threshold);
                memset(RxBuffer,0,sizeof(RxBuffer));
                if(T_Threshold > 0){
                   digitalWrite(T_SWITCH_PIN,HIGH);
                 }else{
                   digitalWrite(T_SWITCH_PIN,LOW);
                 }
                 
                 if(H_Threshold > 0){
                   digitalWrite(H_SWITCH_PIN,HIGH);
                 }else{
                   digitalWrite(H_SWITCH_PIN,LOW);
                 }
        }
            delay(100);
            timCnt++;
}
